package com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.Utils;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.callback.LocationCallBack;
import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.baidu.trace.LBSTraceClient;
import com.baidu.trace.api.entity.EntityInfo;
import com.baidu.trace.api.entity.EntityListRequest;
import com.baidu.trace.api.entity.EntityListResponse;
import com.baidu.trace.api.entity.FilterCondition;
import com.baidu.trace.api.entity.LatestLocation;
import com.baidu.trace.api.entity.OnEntityListener;
import com.baidu.trace.model.CoordType;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/9/10.
 */

public class UserLocation {

    private MapView mapView;
    private BaiduMap baiduMap;
    //获取位置的回调
    private LocationClient locationClient;
    private LocationCallBack locationCallBack;

    // 轨迹服务客户端
    public LBSTraceClient client;
    private EntityListRequest request;
    // 请求标识
    int tag = 1;
    //设置活跃时间
    long activeTime = System.currentTimeMillis() / 1000 - 5*60;
    // 过滤条件
    FilterCondition filterCondition = new FilterCondition();
    // 返回结果坐标类型
    CoordType coordTypeOutput = CoordType.bd09ll;
    // 分页索引
    int pageIndex = 1;
    // 分页大小
    int pageSize = 100;

   private static UserLocation userLocation;
    private ArrayList<String> arrayList;

    private UserLocation(){}

    public static UserLocation getUserLocationInstance(){
        if(userLocation== null){
            userLocation=new UserLocation();
        }


        return userLocation;
    }

    public void getUserLocation(Context context,LocationCallBack locationCallBack){
        SDKInitializer.initialize(context.getApplicationContext());
        this.locationCallBack=locationCallBack;
        mapView=new MapView(context);
        baiduMap=mapView.getMap();

        baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);//普通地图
        // baidumap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);//卫星地图
        baiduMap.setMyLocationEnabled(true);
        locationClient = new LocationClient(context);
        locationClient.registerLocationListener(new MycityLocationListener());
        setLocationOption();
    }

    private boolean isFirstLocate=true;
    //在地图上移动到我的位置
    private void MoveMyLocation(BDLocation bdLocation){
        if(isFirstLocate){
            LatLng latLng = new LatLng(bdLocation.getLatitude(), bdLocation.getLongitude());
            MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(latLng);
            baiduMap.animateMapStatus(update);
            update = MapStatusUpdateFactory.zoomTo(16f);
            baiduMap.animateMapStatus(update);
            isFirstLocate=false;
        }

        MyLocationData.Builder builder = new MyLocationData.Builder();
        builder.latitude(bdLocation.getLatitude());
        builder.longitude(bdLocation.getLongitude());
        MyLocationData build = builder.build();
        baiduMap.setMyLocationData(build);
    }
    private void setLocationOption() {
        LocationClientOption locationClientOption = new LocationClientOption();
        locationClientOption.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//设置模式
        locationClientOption.setOpenGps(true);
        locationClientOption.setCoorType("bd0911");
        locationClientOption.setIsNeedAddress(true);//设置是否需要地址信息，默认不需要,不设置的话bdLocation 为null
        //locationClientOption.setScanSpan(30*1000);//每隔30秒自动更新定位
        locationClient.setLocOption(locationClientOption);
        locationClient.start();
    }



    //查询单个体的实时位置
    public void QueryEntityLocation(Context context, String EntityName, LocationCallBack locationCallBack){
        this.locationCallBack=locationCallBack;
        if(client == null){
            client=new LBSTraceClient(context);
        }
        // 查找当前时间5分钟之内有定位信息上传的entity
        filterCondition.setActiveTime(activeTime);
        arrayList = new ArrayList<>();
        arrayList.clear();
        arrayList.add(EntityName);
        filterCondition.setEntityNames(arrayList);
        // 创建Entity列表请求实例
        if(request == null){
            request = new EntityListRequest(tag, Constant.SERVICE_ID, filterCondition, coordTypeOutput, pageIndex, pageSize);
        }
        // 查询Entity列表
        client.queryEntityList(request, entityListener);


    }
    // 查询单个体的实时位置 监听器 查询结果为百度地图的坐标
    private OnEntityListener entityListener = new OnEntityListener() {
        @Override
        public void onEntityListCallback(EntityListResponse entityListResponse) {
            super.onEntityListCallback(entityListResponse);
            String message = entityListResponse.getMessage();
            List<EntityInfo> entities = entityListResponse.getEntities();
            if(entities ==null || entities.size() ==0){
                return;
            }
            LatestLocation latestLocation = entities.get(0).getLatestLocation();
            String entityName = entities.get(0).getEntityName();
            com.baidu.trace.model.LatLng latLng = latestLocation.getLocation();
            double latitude = latLng.getLatitude();//纬度
            double longitude = latLng.getLongitude();//经度
            Log.i("TRAIL","--单个位置查询--纬度--"+latitude+"---经度-----"+longitude+"----entityName----"+entityName);

            if(!CommonUtil.isZeroPoint(latitude,longitude)){
                locationCallBack.getUserCoordinate(latitude,longitude);
            }



        }
    };

    class MycityLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
           // MoveMyLocation(bdLocation);
            // bdLocation.getProvince();//获取省份   还有其他获取城市、街道等
            String location = bdLocation.getAddrStr();
            Log.i("TRAIL","----位置---"+location);
            locationCallBack.getUserLocation(bdLocation);
        }
    }

  /* public  interface LocationCallBack {

        void getUserLocation(String location);
    }*/
}
